<?php

/*
 * Table/SemaineTable.php
 */

namespace RHX\Model\Table;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use RHX\Model\Object\Semaine;

/**
 * Classe SemaineTable : Représentation de la Table Semaine
 */
class SemaineTable extends AbstractTableGateway {

    /**
     *
     * @var string Nom de la table
     */
    protected $table = 'rh_semaine';

    /**
     * Constructeur
     *
     * @param \Zend\Db\Adapter\Adapter $adapter
     */
    public function __construct(Adapter $adapter) {
	$this->adapter			 = $adapter;
	$this->resultSetPrototype	 = new ResultSet();
	$this->resultSetPrototype->setArrayObjectPrototype(new Semaine());
	$this->initialize();
    }

    /**
     * Récupérer toute les semaines de la table
     *
     * @return Collection
     */
    public function getAll() {
	return $this->select();
    }

    /**
     * Semaine à partir d'une liste
     *
     * @param array $list
     * @return Collection
     */
    public function getSemaines($list, $order = 'id ASC') {
	return $this->select(function(Select $select) use($list, $order) {
			    $where = new Where();
			    $where->in('id', $list);
			    $select->where($where);
			    $select->order($order);
			});
    }

    /**
     * Récupérer une semaine
     * 
     * @param type $id
     * @return Semaine
     * @throws \Exception
     */
    public function get($id) {
	$id	 = (int) $id;
	$row	 = $this->select(array('id' => $id))
		->current();
	if (!$row)
	    throw new \Exception("Erreur 41 : La Semaine #$id n'a pas été trouvée");
	return $row;
    }

    public function save(Semaine $sem) {
	$data	 = array(
	    'libelle'	 => $sem->libelle,
	    'lundi'		 => $sem->lundi,
	    'mardi'		 => $sem->mardi,
	    'mercredi'	 => $sem->mercredi,
	    'jeudi'		 => $sem->jeudi,
	    'vendredi'	 => $sem->vendredi,
	    'samedi'	 => $sem->samedi,
	    'dimanche'	 => $sem->dimanche,
	);
	$id	 = (int) $sem->id;
	if ($id == 0) {
	    $this->insert($data);

	    return $this->lastInsertValue;
	}
	elseif ($this->get($id)) {
	    $this->update($data, array('id' => $id));

	    return $id;
	}
	else {
	    throw new \Exception("Erreur 42 : La Semaine #$id n'a pas été sélectionnée");
	}
    }

    public function erase($id) {
	return $this->delete(array('id' => $id));
    }

    public function getSemaineByDate($date = null, $all = false) {
	if (is_null($date)) {
	    $date = date('Y-m-d');
	}
	$date		 = new \RHX\Helper\RHDateTime($date);
	$dateStr	 = $date->date('Y-m-d');
	$sql		 = "SELECT * FROM rh_semaine WHERE '$dateStr' BETWEEN lundi AND dimanche";
	$result	 = $this->adapter
		->query($sql)
		->execute();
	$semaines	 = array();
	foreach ($result as $row) {
	    $semaines[] = new Semaine($row);
	}

	if (!$all) {
	    if(isset($semaines[0])){
		return $semaines[0];
	    }else{
		$date = new \RHX\Helper\RHDateTime();
		if(!$date->isDay('lundi')){
		    $date->modify('previous monday');
		}
		$semaine = new Semaine();
		$semaine->lundi = $date->date();
		$date->modify('next day');
		$semaine->mardi = $date->date();
		$date->modify('next day');
		$semaine->mercredi = $date->date();
		$date->modify('next day');
		$semaine->jeudi = $date->date();
		$date->modify('next day');
		$semaine->vendredi = $date->date();
		$date->modify('next day');
		$semaine->samedi = $date->date();
		$date->modify('next day');
		$semaine->dimanche = $date->date();
		return $semaine;
	    }
	}
	else {
	    return $semaines;
	}
    }

}

?>
